package com.coffee.core.entity;

import com.coffee.core.util.DTOUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页信息封装类
 *
 * @author Xulg
 */
public class Page<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_CURRENT_PAGE = 1;

    private static final int DEFAULT_PAGE_SIZE = 10;

    private int start = 0;

    /**
     * 页码
     */
    private int currentPage;

    /**
     * sql索引开始位置
     */
    private int startIndex;

    /**
     * 单页数据量
     */
    private int pageSize;

    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 数据总量
     */
    private int amount;

    /**
     * 数据
     */
    private List<T> data;

    //private Map<String, Object> dataMap;

    //private String totalSum = "0";

    public Page(Integer currentPage, Integer pageSize) {
        if (currentPage == null || currentPage <= 0) {
            currentPage = DEFAULT_CURRENT_PAGE;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        // 计算sql的索引
        this.startIndex = (currentPage - 1) * pageSize;
    }

    public int getStart() {
        return (this.currentPage - 1) * this.pageSize;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public int getAmount() {
        return amount;
    }

    /**
     * 设置总数据量大小
     *
     * @param totalDataAmount the total data amount
     */
    public void setAmount(int totalDataAmount) {
        this.amount = totalDataAmount;
        if (pageSize != 0) {
            if (amount % pageSize == 0) {
                this.totalPage = totalDataAmount / pageSize;
            } else {
                this.totalPage = totalDataAmount / pageSize + 1;
            }
        }
    }

    public static <T> Page<T> emptyPageData(int currentPage, int pageSize) {
        if (currentPage <= 0) {
            currentPage = DEFAULT_CURRENT_PAGE;
        }
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        Page<T> page = new Page<>(currentPage, pageSize);
        page.setAmount(0);
        page.setData(new ArrayList<>(0));
        return page;
    }

    /**
     * 将BO数据转为Result数据
     *
     * @param page  the page data
     * @param clazz the result class
     * @param <T>   the page data type
     * @param <R>   tje result type
     * @return the new page data
     */
    public static <T, R> Page<R> convertBO2Result(Page<T> page, Class<R> clazz) {
        if (page == null) {
            return new Page<>(DEFAULT_CURRENT_PAGE, DEFAULT_PAGE_SIZE);
        }
        Page<R> result = new Page<>(page.currentPage, page.pageSize);
        result.amount = page.getAmount();
        result.totalPage = page.getTotalPage();
        result.setData(DTOUtils.convertList(page.getData(), clazz));
        return result;
    }
}
